'''
https://leetcode.cn/problems/delete-n-nodes-after-m-nodes-of-a-linked-list
'''
from typing import Optional


# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def deleteNodes(self, head: Optional[ListNode], m: int, n: int) -> Optional[ListNode]:
        if not head: return None
        cur, pre = head, None
        for _ in range(m):
            pre = cur
            if not cur:
                return head
            cur = cur.next
        for _ in range(n):
            if not cur:
                break
            cur = cur.next
        pre.next = self.deleteNodes(cur, m, n)
        return head

